import moment from "moment";
import BigNumber from "bignumber.js";

export const getItem = (key: string) => {
    return localStorage.getItem(key)
}
export const setItem = (key: string, value: string) => {
    return localStorage.setItem(key, value)
}

export const removeItem = (key: string) => {
    return localStorage.removeItem(key)
}

export const formatMoment = (value: Date, format: string) => {
    return moment(value).format(format);
}

//千分位分隔符
export const formatNumSeparator = (num: any, pointNum = 2) => {
    if ((!num && num !== 0) || num == '-') return '--';
    let arr = (typeof num == 'string' ? parseFloat(num) : num).toFixed(pointNum).split('.');
    let regExp = arr[0].replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
    return arr[1] === undefined ? regExp : `${regExp}.${arr[1]}`

}

//格式化数值
export const formartNumByBigNumber = (num: any, isBig?: boolean) => {
    if (num) {
        if (isNaN(Number(num))) {
            return "0.00"
        } else {
            if (isBig) {
                return BigNumber(BigNumber(num).div(10 ** 18).toFixed(6, 1)).toFormat()
            } else {
                if (BigNumber(BigNumber(num).toFixed(6, 1)).toNumber() < 100 && BigNumber(BigNumber(num).toFixed(6, 1)).toString().indexOf('.') == -1) {
                    return BigNumber(BigNumber(num).toFixed(6, 1)).toFormat() + ".00"
                } else {
                    return BigNumber(BigNumber(num).toFixed(6, 1)).toFormat()
                }
            }

        }
    } else {
        return "0.00"
    }
}


export const toNewAddress = (data: string) => {
    if (data) {
        return data.substring(0, 12) + "...." + data.substring(data.length - 12)
    } else {
        return "--"
    }
}